home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
L' Effet Pommier 3
/
L'Effet Pommier - Volume 03.iso
/
Programmation
/
xComputer 1.2
/
READ ME (with instructions!)
next >
Wrap
Text File
|
1996-01-03
|
22KB
|
426 lines
(January 3, 1996)
xComputer 1.2: A "visible computer" that simulates a computer
with a simple CPU and memory. It can be programmed
in assembly language, and the execution of the
programs can be followed in detail.
by: David Eck
Department of Mathematics and Computer Science
Hobart and William Smith Colleges
Geneva, NY 14456
(E-mail: eck@hws.edu)
(Home page: http://hws3.hws.edu:9000/eck/index.html)
Note: This program was written primarily to be used with my textbook,
_The Most Complex Machine: A Survey of Computers and Computing_.
(Even without the book, though, it can be useful in helping
you learn about how computers work.) More information
about the text, along with several other free programs for
Macintosh computers and a complete lab manual, is available
at the following URL on the World Wide Web:
http://godel.hws.edu/TMCM.html
Restrictions: This program can be freely distributed for private,
non-commercial use. This includes distribution in software archives,
bulletin boards, and CD-ROM software collections. I do ask, however,
that the program not be adopted for use in a computer science course
unless my textbook is also being used for that course.
This file contains information about using the xComputer. It should
be accompanied by the program, xComputer 1.2, by a folder of example
programs, and by an order form for _The Most Complex Machine_.
Part 1: xComputer Basics
-------------------------
The main window for the program contains three sections: The Console,
the Memory, and the Registers. The Memory is a scrolling list that
displays 1024 memory locations, numbered from 0 to 1023. Each location
contains a 16-bit number that can be interpreted either as data or
as a machine language instruction. The Registers section of the
window shows eight registers that are components in the xComputer's
Central Processing Unit. The registers are explained below. Finally,
the Console is for interacting with and controlling the xComputer.
For example, program instructions and data can be entered into
memory using the Console.
You can also have program windows. A program window can contain
an assembly language program that can be assembled and loaded into
memory by using the Load command in the Assembler menu. You should
think of this as a shortcut for typing the program one instruction
at a time on the Console. Another advantage of a program window
is that the program you type into it can be saved in a file and later
re-opened using the Open command in the File menu.
Very simple programs can be entered directly into the console.
Make sure that the box labeled "addr:" contains the address of the
location in memory where you want to put the first program
instruction. (This is usually location zero.) Type the instructions
into the box labeled "data:", one at a time; after typing each
instruction, press return or click on the "Load Memory" button.
If the instruction you typed was legal, it will be entered into
memory and the number in the "addr" box will be incremented.
For example, you could enter the following program to add 17 to
105 and store the answer in location 10:
lod-c 105
add-c 17
sto 10
hlt
(With the default menu settings, you won't actually see these
instructions in memory. The instructions are translated into
machine language and are stored in memory as numbers. However,
the instructions are there and will be executed correctly when
you run the program.)
To run the program, first check that the value in the PC register
is the address of the location that contains the first instruction of
the program. The PC, or program counter, tells the xComputer which
instruction to execute. You can set its value to zero using the
"Set PC=0" command in the Assembler menu. To set it to some other
value, enter the value in the "addr" box and then click on the
"Load PC" button. THIS IS IMPORTANT. The most common and most
frustrating mistake when trying to run a program on xComputer is
simply to forget to tell xComputer where in memory the program is
located!
Once you have set the PC, click on the checkbox labeled "Stop-Clock".
As long as this box is unchecked, the computer is running. If you
prefer, you can use the Run command in the Assembler menu to start
the computer running. As the program runs, you will see the
values in the register changing. The instructions are loaded one-by-one
into the IR, or instruction register, and executed. Soon, the program
halts with the answer in memory location 10.
Here is a summary of what the registers are for and how the xComputer
works: The computer works by "fetching" an instruction from memory
and then "executing" that instruction. It takes the computer several
small operations to fetch and execute each instruction from memory.
These small steps are counted off by the number in the COUNT register.
A fetch-and-execute cycle starts when the value in the COUNT register
changes from 0 to 1. Each time the value changes, another small step
of the cycle takes place. At the end of a fetch-and-execute cycle,
one instruction has been fetched from memory and executed, and the
value in the COUNT register is reset to zero to get ready for fetching
and executing the next instruction. This continues until a HLT instruction
is executed (or until you check the "Stop-Clock" button).
What the computer does at each step is determined entirely by the
contents of several registers: The COUNT register, the IR (instruction
register) and the AC (accumulator). Everything is done mechanically:
the numbers in these registers cause certain "control wires" to
be turned on and off, and this in turn causes numbers to be moved from
place to place, sums to be computed, etc. For details, see Chapter 3 of
the textbook, _The Most Complex Machine_.
The individual registers work as follows:
ADDR register: The address register holds the address of a
location in memory. This is the location that can be
stored into or read from.
PC register: The program counter contains the address in memory
of the next program instruction to be executed. The PC is
ordinarily incremented by 1 during each fetch-and-execute cycle.
(A JMP instruction simply changes the value stored in the PC,
so that the computer "jumps" to a different location in the
program.)
IR register: The instruction register holds a program instruction
while it is being executed. This is where an instruction is
put when it is "fetched" from memory.
COUNT register: As explained above, the COUNT register counts
off the individual operations in each fetch-and-execute cycle.
AC register: The accumulator holds a number that is being used
in the current calculation. When a number is loaded from
memory, it is put in the AC. When a number is "added", it
is added to the value currently in the AC, and the result is
put back into the AC. Etc.
FLAG register: The flag register contains a single bit that
can give extra information about a calculation. For example,
when two 16-bit numbers are added, the final "carry" into
the 17-th column is stored in the FLAG register. When a
shift operation is performed on the AC, the extra bit that
is shifted off the end is placed into the FLAG register.
X and Y registers: These hold numbers that are to be used in
a calculation. For example, when two numbers are to be
added, they are placed into X and Y. (The Y register is
also used as a temporary storage place in a few cases.)
Part 2: Menu Operations
------------------------
The commands in the File, Edit and Windows menus should be
reasonably self-explanatory.
The Assembler menu starts with two commands for loading programs
into memory. THESE CAN ONLY BE USED IF THE xComputer IS NOT RUNNING.
The first command is used to load a program from the frontmost
window on the screen. The Load File command lets you select
a file so that you can load its contents into the xComputer's
memory without first opening the file in its own window.
The Run and Stop Clock commands are equivalent to unchecking and
checking the Stop-Clock box in the xComputer window.
The two Disassemble commands will take the current contents of
memory and turn them into an assembly language program in a
window. Loading that window into memory will restore the contents
of memory exactly.
The Clear Memory and Set PC=0 commands are self-evident.
The Options menu controls many details of how the xComputer runs and
what you see on the screen as it runs. It contains three sub-menus and
three other options that can be turned off and on.
The Run Speed sub-menu determines how fast the xComputer runs.
As the fastest speed, the registers are hidden so that the computer
can run as fast as possible. At the two slowest speeds, the
computer doesn't really "run" by itself at all. Instead, a
button labeled "Next" appears in the Console, and you have to
click repeatedly on this button to make the computer run. This
allows you to observe the operation of xComputer in great detail.
The Memory Display sub-menu determines what you see in the Memory
area of the xComputer window. You can see the contents of each
memory location as a binary number, a signed integer (in the
range -32768 to 32767), an unsigned integer (in the range 0 to
65535), an assembly language instruction, or as a pair of ASCII
characters. A "Graphics" display option will show the entire memory
at once, as a grid of pixels. That is, each of the 16-times-1024 bits
in memory will be represented as a pixel that is off if the bit is
zero and is on if the bit is one. (It can be particularly effective
to run the computer at full speed and watch the bits "dance" in
the Graphics display.) The final option in the Memory Display menu
is "Show Control Wires Instead". With this option, the Memory
display is replaced by a list of the xComputer's control wires.
(Turning control wires on and off is what makes things happen
in the xComputer.) As a program is executed, the control wires that
are turned on at any given time are hilited. You might not find this
particularly useful unless you have read _The Most Complex Machine_.
The Register Display sub-menu is similar to the Memory Display
menu, but not as useful. It should be self-evident.
If the Autoscroll Memory option is turned on, then each time the
value in the ADDR register is changed, the memory will be automatically
scrolled so that the indicated memory location is lined up with the
ADDR register. This might make it easier for you to see how numbers
are loaded from and stored into memory.
Not every 16-bit number represents a legal assembly language instruction.
Ordinarily, xComputer simply ignores illegal instructions. If you
turn on the Halt on Illegal Instruction option, however, xComputer will
stop whenever it loads an illegal instruction into the IR register.
This might make it easier for you to "debug" your programs.
The "Use I/O Services" option essentially modifies the xCompuer so that
it can do simple input and output operations. When you turn on this
option, an "Output" list appears in the Console. Then, when you
run a program, an "Input queue" appears as well. Several additional
assembly language instructions become available when I/O services are on.
The purpose is to illustrate several advanced aspects of computer
operation in a simplified form. This is explained in more detail
below and in the sample programs.
Part 3: The Assembly Language of xComputer
-------------------------------------------
A program for the xComputer consists of instructions and data. Since
everything is actually stored in memory in the form of 16-bit numbers,
the only real distinction between instructions and data is the way
they are used. You should really think in terms of having several
different ways of writing 16-bit numbers. Any of the following ways
are valid anywhere in a program:
-- An assembly language instruction, from the list of instructions
given below. (The instruction itself is represented in the
leftmost 6 bits of the 16-bit number. The remaining 10 bits
specify a number between 0 and 1023 that is used as a
parameter for the instruction.)
-- A number in the range -32768 to 65535. 16-bit numbers can be used
to represent either unsigned integers in the range 0 to 65535 or
signed integers in the range -32768 to 32767. It's just a matter
of interpretation. You can enter numbers in either form.
-- A binary number consisting of a B followed by from 1 to 16 bits.
For example: B101110100111. (This is the most "realistic"
representation.)
-- A character preceded by a single left quote, such as: 'z
This represents the ASCII code of the character.
You can type any of these things into the "data" box in xComputer's Console.
You can also use them in programs typed into program windows. (Only one
instruction is allowed per line.)
Programs in windows can contain several other features. A semicolon and
everything following it on a line is considered a comment and is ignored
when the program is loaded. Any program item can be preceded by a label and
a colon. The label, which consists of letters and digits beginning with a
letter, then becomes a name for the memory location that contains that item
when the program is loaded. In fact, the label is treated as a NUMBER that
gives the address of that location, and it can be used in a program wherever
a number could be used. For example:
start: lod-c 0 ; "start" is a label
lod-c start ; loads the number "start" into the AC
jmp start ; jumps to location "start"
A program item can be preceded by a number followed by a #. This is a
repetition count and is the same as typing the item the specified number
of times. For example, "25# 17" puts a 17 in each of the next 25
memory locations. "4# SHL" is equivalent to four SHL instructions.
Ordinarily, a program is loaded into consective memory locations starting at
location 0. However, you can specify where loading is to take place
with a @ followed by an address. For example, "@100" specifies that the
next item is to go into memory location 100. (Items following that one
will then go into location 101, 102, etc.) A special form of this
command, "@PC" is provided to load a number into the PC register. THIS
IS DONE ONLY ONCE, WHEN THE PROGRAM IS LOADED. For example, "@PC 100"
will put a 100 into the PC when the program is loaded. Presumably,
you would use this because your program starts at location 100, so you
would like the PC to start with that value.
Here is a list of the assembly language instructions that can be
used for the xComputer. In these instructions, X represents a
number between 0 and 1023. In a program, X can be given as an
ordinary decimal number, as a binary number preceded by a B
(for example: B1011), or as a single ASCII character preceded by
a right single quote mark (for example: 'A or '?). It can also
be given as a symbolic label, provided that the name is used
somewhere in the program as a label for a memory location. If a
number is given for an instruction that does not require it,
such as HLT or SHL, the number is ignored. Note that no distinction
is made between upper and lower case in instruction names or labels.
Instruction Meaning
-------------- ------------------------------------------------
ADD X Add the number in memory location X to the AC
ADD-C X Add the number X to the AC
ADD-I X Let Y be the contents of memory location X, and
add the number in location Y to the AC
SUB X Subtract the number in memory location X from the AC
SUB-C X Subtract the number X from the AC
SUB-I X Let Y be the contents of memory location X, and
subtract the number in location Y from the AC
AND X Bitwise AND the number in memory location X with the AC
AND-C X Bitwise AND the number X with the AC
AND-I X Let Y be the contents of memory location X, and
bitwise AND the number in location Y with the AC
OR X Bitwise OR the number in memory location X with the AC
OR-C X Bitwise OR the number X with the AC
OR-I X Let Y be the contents of memory location X, and
bitwise OR the number in location Y with the AC
NOT Apply a bitwise NOT to the AC
INC Add 1 to the AC
DEC Subtract 1 from the AC
SHL Shift the AC left one bit
SHR Shift the AC right one bit
LOD X Load the number in location X into the AC
LOD-C X Load the number X into the AC
LOD-I X Let Y be the contents of the memory location X, and
load the number from location Y into the AC
STO X Store the value in AC into memory location X
STO-I X Let Y be the contents of memory location X, and
store the value in AC into location Y
JMP X Jump to location X (that is, store X into the PC,
so that the next instruction will be loaded from X)
JMP-I X Let Y be the contents of memory location X, and
jump to location Y
JMZ X If the value in the AC is zero, then jump to location X
JMP-I X If the value in the AC is zero, then let Y be the contents
of memory location X, and jump to location Y
JMN X If the value in the AC is negative, then jump to
location X
JMN-I X If the value in the AC is negative, then let Y be the
contents of memory location X, and jump to location Y
JMF X If the value in the FLAG register is one, then jump to
location X
JMF-I X If the value in the FLAG register is one, then let Y be the
contents of memory location X, and jump to location Y
HLT Halt. That is, stop the xComputer by turning on the
STOP CLOCK control wire
In addition, when the "Use I/O Services" option is on, the following
instructions can be used:
GTC Get Character. When the xComputer is running with I/O
services on and the user types a character, that
character is put into an "input queue". The GTC
instruction removes the next character from the
queue and puts its ASCII code into the AC register.
If no character is available in the queue, a
zero is put into the AC.
PTC Put Character. Writes the contents of the AC register to
the output list (which appears in the Console of the
xComputer window when I/O services are on) in the form
of a character. If the number in the AC is greater than
255, then it is written as two characters.
PTI Put Integer. Writes the contents of the AC register to
the output list in the form of a signed integer.
PTU Put Unsigned. Writes the contents of the AC to the output
list in the form of an unsigned integer.
PTB Put Binary. Writes the contents of the AC to the output
list in the form of a binary number.
INH X Interrupt Handler at X. When the user types a character,
a "keyboard interrupt" is generated. By default, the
xComputer "handles" the keyboard interrupt by placing the
character into the input queue. The INH instruction can
be used to set up further processing of keyboard
interrupts. Specifically, if an INH X instruction has
been executed, when a keyboard interrupt occurs, the
xComputer will save the state of the current computation,
place the character into the keyboard queue, and jump
to location X. It will execute instructions starting
at location X until an RTI instruction is encountered.
Then, it will restore the saved state and pick up
where it left off when the interrupt occured. (Unless,
that is, another key has been pressed -- in that case,
it will handle the "pending interrupt" by going back
to location X to handle the next character. Note that
an interrupt handler MUST remove a character from the
input queue with a GTC command and MUST end with an
RTI instruction.) The command "INH 1023" can be used
to CANCEL the current interrupt handler and restore the
default behavior.
RTI Return from Interrupt. Ends a keyboard interrupt handler,
as explained above under the INH command.